package com.ttbj.controller.location;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ttbj.product.api.DriverFacade;
import com.ttbj.product.api.LocationFacade;
import com.ttbj.product.api.OrderFacade;
import com.ttbj.product.api.WaybillFacade;
import com.ttbj.product.bean.dto.CoordDto;
import com.ttbj.product.bean.dto.DriverDto;
import com.ttbj.product.bean.dto.OrderDto;
import com.ttbj.product.bean.dto.PointDto;
import com.ttbj.product.bean.dto.TruckDto;
import com.ttbj.product.bean.dto.UserDto;
import com.ttbj.product.bean.dto.ViewData;
import com.ttbj.product.bean.dto.WaybillDto;
import com.ttbj.util.Assert;
import com.ttbj.util.ViewModelUtil;

/**
 * 地图位置定位，轨迹等controller
 * 
 * @author Medivh
 *
 */
@Controller()
@RequestMapping("/applet")
public class LocationController {

	@Autowired
	private LocationFacade locationFacade = null;

	@Autowired
	private DriverFacade dirverFacade = null;

	@Autowired
	private WaybillFacade waybillFacade = null;

	@Autowired
	private ThreadPoolTaskExecutor taskExecutor = null;

	/**
	 * 获取订单所属的司机轨迹
	 * 
	 * @param request
	 * @param response
	 * @param openId
	 * @param orderId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/track/all.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData allTrack(HttpServletRequest request, HttpServletResponse response, String openId, Long orderId) throws Exception {
		List<WaybillDto> allWayBill = waybillFacade.allWayBill(orderId);
		if (null == allWayBill || allWayBill.isEmpty()) {
			return ViewModelUtil.buildSuccess("成功");
		}
		String waybillInsTime = allWayBill.get(0).getWaybillInsTime();
		List<String> entitys = new ArrayList<String>();
		List<Map<String, List<PointDto>>> points = locationFacade.allTrack(entitys, waybillInsTime);
		return ViewModelUtil.buildSuccessPage("成功", points, points.size());
	}

	/**
	 * 获取多辆车最后一次坐标
	 * @param request
	 * @param response
	 * @param openId
	 * @param orderId
	 * @throws Exception
	 */
	@RequestMapping(value = "/location/all.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData allLocation(HttpServletRequest request, HttpServletResponse response, String openId, Long orderId) throws Exception {
		List<WaybillDto> allWayBill = waybillFacade.allWayBillByNonRefuse(orderId);
		if (null == allWayBill || allWayBill.isEmpty()) {
			return ViewModelUtil.buildSuccess("成功");
		}
		List<CoordDto> list = new ArrayList<>();
		for (WaybillDto waybillDto : allWayBill) {
			Long waybillDriver = waybillDto.getWaybillDriver();
			System.out.println("waybillDriver"+waybillDriver);
			if (null != waybillDriver) {
				DriverDto driver = dirverFacade.findDriver(new DriverDto(waybillDriver), 1);
				if ( Assert.assertNonNullField0(driver, "driverEntity","driverTruck")) {
					Future<TruckDto> submit = taskExecutor.submit(()-> dirverFacade.getTruck(driver.getDriverTruck()));
					CoordDto dto = locationFacade.getlatestpoint(driver.getDriverEntity());
					TruckDto truckDto = submit.get();
					System.out.println("车辆信息:{}"+truckDto);
					if(null != dto && Assert.assertNonNullField0(truckDto, "truckCarnum")){
						dto.setTruck(truckDto.getTruckCarnum());
					}
					list.add(dto);
				}
			}
		}
		return ViewModelUtil.buildSuccessPage("成功", list, list.size());
	}

}
